﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;

using Ivy.Common.Framework.Base;
using Ivy.Common.Framework.DataAccess;
using Ivy.UserRegistration.Entity;

namespace Ivy.UserRegistration.DataAccess
{
    public class Sys_SessionLogDao : IDaoInterface<Sys_SessionLog, Sys_SessionLogCriteria>
    {
        #region  Variable & constant
        #endregion  Variable & constant

        #region Constructor & Destructor
        #endregion Constructor & Destructor

        #region Properties
        #endregion Properties

        #region Public Methods
        public bool Insert(Sys_SessionLog Sys_SessionLogEntity)
        {
            bool IsOperationSuccessful = false;
            if (Sys_SessionLogEntity.EntityState == EntityState.New)
            {
                DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
                DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
                CmdObj.CommandText = "Dasideen_Sys_SessionLog_Insert";
                CmdObj.CommandType = CommandType.StoredProcedure;

                TranslateSys_SessionLogFromEntityToDatabaseParameters(CmdObj, Sys_SessionLogEntity);

                DatabaseCommon.InsertStoredProcForParameterDatEntityBase(CmdObj, Sys_SessionLogEntity);

                DataAccessProviderUtility.AddParameter(CmdObj, "@SessionLogPrimaryID", ParameterDirection.Output, DbType.Int32, DBNull.Value);
                DataAccessProviderUtility.AddParameter(CmdObj, "@Sys_DateOfCreation", ParameterDirection.Output, DbType.DateTime, DBNull.Value);

                CmdObj.Connection = DbConn;
                DbConn.Open();
                CmdObj.Prepare();
                int _NoOfRecord = CmdObj.ExecuteNonQuery();
                if (_NoOfRecord == 1)
                {
                    Sys_SessionLogEntity.SessionLogPrimaryID = Convert.ToInt32(CmdObj.Parameters["@SessionLogPrimaryID"].Value);
                    Sys_SessionLogEntity.Sys_DateOfCreation = Convert.ToDateTime(CmdObj.Parameters["@Sys_DateOfCreation"].Value);
                    Sys_SessionLogEntity.UserSessionStartTime = Sys_SessionLogEntity.Sys_DateOfCreation;
                    Sys_SessionLogEntity.Sys_DateOfModification = Sys_SessionLogEntity.Sys_DateOfCreation;
                    Sys_SessionLogEntity.EntityState = EntityState.UnChanged;
                    IsOperationSuccessful = true;
                }
                CmdObj.Dispose();
                DbConn.Close();
            }
            else
            {

                throw new Exception("For Inserting new Sys_SessionLog entity data to database,[EntityState] attribute should be [New]");
            }

            return IsOperationSuccessful;
        }

        public bool Update(Sys_SessionLog Sys_SessionLogEntity)
        {
            bool IsOperationSuccessful = false;

            if (Sys_SessionLogEntity.EntityState == EntityState.Changed)
            {
                DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
                DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
                CmdObj.CommandText = "Dasideen_Sys_SessionLog_Update";
                CmdObj.CommandType = CommandType.StoredProcedure;

                DataAccessProviderUtility.AddParameter(CmdObj, "@SessionLogPrimaryID", ParameterDirection.Input, DbType.Int32, Sys_SessionLogEntity.SessionLogPrimaryID);

                TranslateSys_SessionLogFromEntityToDatabaseParameters(CmdObj, Sys_SessionLogEntity);
                DatabaseCommon.UpdateStoredProcForParameterDatEntityBase(CmdObj, Sys_SessionLogEntity);

                DataAccessProviderUtility.AddParameter(CmdObj, "@Sys_DateOfModification", ParameterDirection.Output, DbType.DateTime, DBNull.Value);

                CmdObj.Connection = DbConn;
                DbConn.Open();
                CmdObj.Prepare();
                int _NoOfRecords = CmdObj.ExecuteNonQuery();
                if (_NoOfRecords == 1)
                {
                    Sys_SessionLogEntity.Sys_DateOfModification = Convert.ToDateTime(CmdObj.Parameters["@Sys_DateOfModification"].Value);
                    Sys_SessionLogEntity.EntityState = EntityState.UnChanged;
                    IsOperationSuccessful = true;
                }

                CmdObj.Dispose();
                DbConn.Close();
            }
            else
            {
                throw new Exception("For Updating existing Sys_SessionLog entity data to database, [EntityState] attribute should be [Changed]");
            }

            return IsOperationSuccessful;
        }

        public bool Delete(Sys_SessionLog DataEntity)
        {
            throw new NotImplementedException();
        }

        public Sys_SessionLog GetByID(string ID)
        {
            throw new NotImplementedException();
        }

        public List<Sys_SessionLog> GetAll()
        {
            List<Sys_SessionLog> lSys_SessionLogList = new List<Sys_SessionLog>();

            DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
            DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
            CmdObj.CommandText = "Dasideen_Sys_SessionLog_GetAll";
            CmdObj.CommandType = CommandType.StoredProcedure;

            CmdObj.Connection = DbConn;
            DbConn.Open();
            CmdObj.Prepare();

            DbDataReader _DataReader = CmdObj.ExecuteReader();
            if (_DataReader.HasRows)
            {
                while (_DataReader.Read())
                {
                    Sys_SessionLog _TempSys_SessionLog = new Sys_SessionLog(); //IMPORTANT NOTE:- Create instance only when required.
                    this.TranslateSys_SessionLogFieldsFromReaderToEntity(_TempSys_SessionLog, _DataReader);
                    lSys_SessionLogList.Add(_TempSys_SessionLog);
                }
            }
            CmdObj.Dispose();
            DbConn.Close();


            return lSys_SessionLogList;
        }

        public List<Sys_SessionLog> GetByCriteria(Sys_SessionLogCriteria CriteriaEntity)
        {
            throw new NotImplementedException();
        }
        #endregion Public Methods

        #region Events
        #endregion Events

        #region Private Methods
        private void TranslateSys_SessionLogFieldsFromReaderToEntity(Sys_SessionLog pSys_SessionLog, DbDataReader pDbDataReader)
        {

            pSys_SessionLog.SessionLogPrimaryID = Convert.ToInt32(pDbDataReader["SessionLogPrimaryID"]);
            pSys_SessionLog.UserSessionID = Convert.ToString(pDbDataReader["UserSessionID"]);
            pSys_SessionLog.UserIPAddress = Convert.ToString(pDbDataReader["UserIPAddress"]);
            pSys_SessionLog.UserSessionStartTime = Convert.ToDateTime(pDbDataReader["UserSessionStartTime"]);

            if (!pDbDataReader.IsDBNull(pDbDataReader.GetOrdinal("UserSessionEndTime")))
            {
                pSys_SessionLog.UserSessionEndTime = Convert.ToDateTime(pDbDataReader["UserSessionEndTime"]);
            }
            else
            {
                pSys_SessionLog.UserSessionEndTime = null;
            }
            pSys_SessionLog.UserBrowserName = Convert.ToString(pDbDataReader["UserBrowserName"]);
            pSys_SessionLog.UserKey = Convert.ToInt32(pDbDataReader["UserKey"]);


            DatabaseCommon.TranslateSystemFieldsFromReaderToEntity(pSys_SessionLog, pDbDataReader);

        }
        private void TranslateSys_SessionLogFromEntityToDatabaseParameters(DbCommand pDbCommand, Sys_SessionLog pSys_SessionLogEntity)
        {
            DataAccessProviderUtility.AddParameter(pDbCommand, "@UserSessionID", ParameterDirection.Input, DbType.String, pSys_SessionLogEntity.UserSessionID);
            DataAccessProviderUtility.AddParameter(pDbCommand, "@UserIPAddress", ParameterDirection.Input, DbType.String, pSys_SessionLogEntity.UserIPAddress);
            DataAccessProviderUtility.AddParameter(pDbCommand, "@UserSessionStartTime", ParameterDirection.Input, DbType.DateTime, pSys_SessionLogEntity.UserSessionStartTime);
            DataAccessProviderUtility.AddParameter(pDbCommand, "@UserSessionEndTime", ParameterDirection.Input, DbType.DateTime, pSys_SessionLogEntity.UserSessionEndTime);
            DataAccessProviderUtility.AddParameter(pDbCommand, "@UserBrowserName", ParameterDirection.Input, DbType.String, pSys_SessionLogEntity.UserBrowserName);
            DataAccessProviderUtility.AddParameter(pDbCommand, "@UserKey", ParameterDirection.Input, DbType.Int32, pSys_SessionLogEntity.UserKey);
        }
        #endregion Private Methods
        
    }
}
